package com.linkwings.projectsimple.http.action;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.oxm.xstream.XStreamMarshaller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.xml.MarshallingView;

import com.linkwings.projectsimple.bean.TestBean;
import com.linkwings.projectsimple.pojo.SampleUser;
import com.linkwings.projectsimple.service.UserService;


@Controller
@RequestMapping("/user/api")
public class UserApi{

	/*private void initBean(List testlist,Map testMap){
		TestBean tb=new TestBean("a1", "p1", new Date());
		TestBean tb2=new TestBean("a2", "p2", new Date());
		TestBean tb3=new TestBean("a3", "p3", new Date());
		testlist.add(tb);
		testlist.add(tb2);
		testlist.add(tb3);
		testMap.put("a1", tb);
		testMap.put("a2", tb2);
		testMap.put("a3", tb3);
	}*/


		private UserService userService=null;




		//uploadfile 在ie 下，不支持post方式跳转返回数据，如果是纯ajax不跳转，可以支持，但是extjs post 文件用了隐藏ifream，页面跳转，导致出错，只是ie会这样
		//所以这里用传统的ajax方式,而spring官方的处理方式是配置一个filter，见showcase的flashmapfilter
		@RequestMapping(value="/uploadfile",method=RequestMethod.POST)
		public void processUpload(@RequestParam MultipartFile file,@RequestParam String filedesc, HttpServletRequest request,HttpServletResponse response) throws IOException {
			String message = "File '" + file.getOriginalFilename() + "'  size ["+file.getSize()/1024+"kb]   uploaded successfully , desc is -> "+filedesc;
			// prepare model for rendering success message in this request
			System.out.println(message);
			//request.getSession().setAttribute("fileupRES", message);
			//return "redirect:/user/api/upres";
			//return message;
			response.setContentType("text/html;charset=UTF-8");//中文乱码问题
			response.getWriter().write(/*"[REQUEST-ACCEPT = "+request.getHeader("Accept")+"]"+*/message);
		}

		  /* @RequestMapping(value="/upres", method=RequestMethod.GET)
		   @ResponseBody
		   //@ModelAttribute
		   public String upres(HttpServletRequest request){
		      return request.getSession().getAttribute("fileupRES").toString();
		   }*/


	   @RequestMapping(value="/list", method=RequestMethod.GET)
	   @ResponseBody //如果使用后缀名不能有 ResponseBody
	   //@ModelAttribute
	   public  List<SampleUser> jsonlist(HttpServletRequest request){
		 List<SampleUser> list = userService.findListUser();
		 //request.setAttribute("list", list);

		 for(SampleUser user:list){
			// initBean(user.getTestlist(),user.getTestMap());
		 }

	     return list;
	   }

	   @RequestMapping(value="/{id}/show", method=RequestMethod.GET)
	   @ResponseBody
	   //@ModelAttribute
	   public SampleUser show(@PathVariable int id,HttpServletRequest request){
		  SampleUser user = userService.findUserById(id);
		 // initBean(user.getTestlist(),user.getTestMap());

	      return user;
	   }

	   @RequestMapping(value="/postUser", method=RequestMethod.POST)
	   @ResponseBody
	   public String postUser(@RequestBody SampleUser user){
		   return "success! user to string -> "+user.toString();
	   }

	   @RequestMapping(value="/postListUser", method=RequestMethod.POST)
	   @ResponseBody
	   public String postUser(@RequestBody List<SampleUser> list){ //xstream is ok but jackson fail
		   String str="";
		   for(SampleUser user:list){
			   str=str+user.toString()+" ";
		   }
		   return "success! list size["+list.size()+"] to string -> "+str;
	   }

	   /*@RequestMapping(value="/postListUser", method=RequestMethod.POST)
	   @ResponseBody
	   public String postUser(@RequestBody List<LinkedHashMap> list){
		   String str="";
		   for(LinkedHashMap map:list){
			   Iterator it=map.keySet().iterator();
			   str=str+" [";
			   while(it.hasNext()){
				   str=str+it.toString()+"="+map.get(it.toString())+" , ";
			   }
			   str=str+"] ";
		   }
		   return "success! list size["+list.size()+"] to string -> "+str;
	   }*/

	   @RequestMapping(value="/{id}/showstr", method=RequestMethod.GET)
	   @ResponseBody
	   //@ModelAttribute
	   public String showstr(@PathVariable int id,HttpServletRequest request){
		  SampleUser user = userService.findUserById(id);
		  user.setPassword(null);
	      return "Your Query user name is ["+user.getNickName()+"]";
	   }

	   @RequestMapping(value="/showbean", method=RequestMethod.GET)
	   @ResponseBody
	   //@ModelAttribute
	   public TestBean showbean(){
		  TestBean tb = new TestBean("babyface", "psw123", new Date());
	      return tb;
	   }

	   @RequestMapping(value="/showlistbean", method=RequestMethod.GET)
	   @ResponseBody
	   //@ModelAttribute
	   public List<TestBean> showlistbean(){
		   List<TestBean> list = new ArrayList<TestBean>();
		  //TestBean tb = ;
		  list.add(new TestBean("babyface", "psw123", new Date()));
		  list.add(new TestBean("lannerk", "psw456", new Date()));
		  list.add(new TestBean("lanyxu", "psw789", new Date()));
	      return list;
	   }

	  /* @RequestMapping(value="/{id}/show", method=RequestMethod.GET)
	   @ResponseBody
	   public Map<String,Object> jsonshow(@PathVariable int id,HttpServletRequest request){
		  SampleUser user = userService.findUserById(id);
		  Map<String,Object> m = new HashMap<String,Object>();
		  m.put("id", user.getId());
		  m.put("username", user.getLoginName());
		  m.put("email", user.getEmail());
		  m.put("date", user.getBirthDatetime());
	      return m;
	   }*/


	  /* @RequestMapping(value="/{id}/show", method=RequestMethod.GET)
	   //@ResponseBody
	   public ModelAndView jsonshow(@PathVariable int id,HttpServletRequest request){
		  SampleUser user = userService.findUserById(id);
	     return new ModelAndView("/user/view", "user",user);
	   }*/

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
